package Bean;

import com.sun.istack.Builder;
import com.sun.istack.NotNull;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import javax.persistence.*;
import java.sql.Timestamp;
import java.text.ParseException;

@Entity
@Table(name = "loanbooklogrecord", schema = "bookloansystem", catalog = "")
public class LoanBookLogRecord {

    int id;
    int bookId;
    int userId;
    Timestamp loanTime;//订单发起时间
    int day;
    String state;

    public LoanBookLogRecord(){
        loanTime=new Timestamp(System.currentTimeMillis());
    }


    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Column(name = "bookid")
    @Basic
    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    @Column(name = "userid")
    @Basic
    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
    @Column(name = "loantime")
    @Basic
    public Timestamp getLoanTime() {
        return loanTime;
    }

    public void setLoanTime(Timestamp loanTime) {
        this.loanTime = loanTime;
    }
    @Column(name = "day")
    @Basic
    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
    @Column(name = "state")
    @Basic
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }


    //剩余天数
    public int remainingDays()  throws Exception {
        int days = 0;
        try {
            days = this.day+ TimeTools.Remainder_days(this.getLoanTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        return days<0?0:days;
        return days;
    }
    public String formatloanTime(){
        return TimeTools.timeStamp2Date(getLoanTime(), null);
    }
    public String bookname(){
        Session session = HibernateTools.getSession();
        BookInfoEntity bookInfoEntity= null;
        try {
            bookInfoEntity = session.get(BookInfoEntity.class, bookId);
        } catch (Exception e) {
            return "图书不存在";
        }
        if (bookInfoEntity==null){
            return "图书不存在";
        }
        return bookInfoEntity.getBook_name();
    }
}
